package com.liu.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.eduService.entity.EduCourse;
import com.liu.eduService.entity.EduTeacher;
import com.liu.eduService.entity.vo.TeacherQuery;
import com.liu.eduService.mapper.EduTeacherMapper;
import com.liu.eduService.service.EduCourseService;
import com.liu.eduService.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.serviceBase.exception.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author LSJ
 * @since 2021-12-14
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduCourseService courseService;

    @Override
    public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
        // 查询条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        System.out.println("========================" + teacherQuery);
        if (Objects.isNull(teacherQuery) || teacherQuery == null) {
            baseMapper.selectPage(pageParam, wrapper);
        } else {

            // 获取条件：
            String name = teacherQuery.getName();
            Integer level = teacherQuery.getLevel();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();
//        Page<EduTeacher> pageParam = new Page<>(page,limit);


            if (!Objects.isNull(name)) {
                wrapper.like("name", name);
            }
            if (!Objects.isNull(level)) {
                wrapper.eq("level", level);
            }
            if (!Objects.isNull(begin)) {
                wrapper.ge("gmt_create", begin);
            }
            if (!Objects.isNull(end)) {
                wrapper.le("gmt_modified", end);
            }
            //排序
            wrapper.orderByDesc("gmt_create");
            baseMapper.selectPage(pageParam, wrapper);
        }
    }

    @Override
    public Map<String, Object> queryFrontTeacher(Page<EduTeacher> teacherPage) {

        if (teacherPage==null){
            throw  new MyException(20001,"queryFrontTeacher方法teacherPage为空");
        }
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        // 分页查询
        baseMapper.selectPage(teacherPage,wrapper);


        List<EduTeacher> records = teacherPage.getRecords();
        long current = teacherPage.getCurrent();
        long pages = teacherPage.getPages();
        long size = teacherPage.getSize();
        long total = teacherPage.getTotal();
        // 是否有下一页
        boolean hasNext = teacherPage.hasNext();

        // 是否有上一页
        boolean hasPrevious = teacherPage.hasPrevious();

        Map<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;

    }

    @Override
    public Map<String, Object> getTeacherAllInfo(String id) {
        EduTeacher eduTeacher = baseMapper.selectById(id);
        List<EduCourse> courseList = courseService.getCourseListByTeacherId(id);
        Map<String, Object> map = new HashMap<>();
        map.put("eduTeacher",eduTeacher);
        map.put("courseList",courseList);
        return map;
    }
}
